import java.util.PriorityQueue;

class MedianFinder {
    private PriorityQueue<Integer> left;
    private PriorityQueue<Integer> right;
    public MedianFinder() {
        left = new PriorityQueue<>((a, b) -> b - a);
        right = new PriorityQueue<>((a, b) -> a - b);
    }

    public void addNum(int num) {
        int m = left.size();
        int n = right.size();
        if (m == n) {
            if (m == 0 || num <= left.peek()) {
                left.offer(num);
            } else if (num > left.peek()) {
                right.offer(num);
                left.offer(right.poll());
            }
        } else if (m == n + 1) {
            if (num <= left.peek()) {
                left.offer(num);
                right.offer(left.poll());
            } else if (num > left.peek()) {
                right.offer(num);
            }
        }
    }

    public double findMedian() {
        if (left.size() == right.size()) return (double) (left.peek() + right.peek()) / 2;
        else return (double) left.peek();
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */